package com.xlh.Utils;

import android.content.Context;
import android.graphics.Color;

import com.xlh.Model.ColorConfModel;
import com.xlh.bean.AppColor;
import com.xlh.interf.IAppColor;
import com.xlh.lib.Core.FFCursorHelper;
import com.xlh.outside.MainActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Constant {
	//正则匹配值
	public static List<String> colorRegExList;
	private static Map<String , String> mColorMap;

	public static Map<String , String> getColorMap() {

		if(mColorMap == null) {
			mColorMap = new HashMap<String, String>();
		}

		addColor();
		return mColorMap;
	}

	public static List<String> mColorList;

	public static List<String> getColorList() {

		if(mColorList == null) {
			mColorList = new ArrayList<String>();
		}

		insertColor();

		return mColorList;
	}

	public static String replaceColor(String src) {

		Pattern pattern;
		Matcher matcher;
		String retStr = "";

		if (Constant.colorRegExList == null) {
			Constant.colorRegExList = new ArrayList<String>();
			String bootStr = ESC + "\\[";
			Constant.colorRegExList.add("("+ESC + "\\[30m)");
			Constant.colorRegExList.add("("+bootStr + "31m)");
			Constant.colorRegExList.add("("+bootStr + "32m)");
			Constant.colorRegExList.add("("+bootStr + "33m)");
			Constant.colorRegExList.add("("+bootStr + "34m)");
			Constant.colorRegExList.add("("+bootStr + "35m)");
			Constant.colorRegExList.add("("+bootStr + "36m)");
			Constant.colorRegExList.add("("+bootStr + "37m)");

			Constant.colorRegExList.add("("+bootStr + "1;31m)");
			Constant.colorRegExList.add("("+bootStr + "1;32m)");
			Constant.colorRegExList.add("("+bootStr + "1;33m)");
			Constant.colorRegExList.add("("+bootStr + "1;34m)");
			Constant.colorRegExList.add("("+bootStr + "1;35m)");
			Constant.colorRegExList.add("("+bootStr + "1;36m)");
			Constant.colorRegExList.add("("+bootStr + "1;37m)");

			Constant.colorRegExList.add("("+bootStr + "41;1m)");
			Constant.colorRegExList.add("("+bootStr + "42;1m)");
			Constant.colorRegExList.add("("+bootStr + "43;1m)");
			Constant.colorRegExList.add("("+bootStr + "44;1m)");
			Constant.colorRegExList.add("("+bootStr + "45;1m)");
			Constant.colorRegExList.add("("+bootStr + "46;1m)");
			Constant.colorRegExList.add("("+bootStr + "47;1m)");
			Constant.colorRegExList.add("("+bootStr + "47;1m)");

			Constant.colorRegExList.add("("+bootStr + "40m)");
			Constant.colorRegExList.add("("+bootStr + "41m)");
			Constant.colorRegExList.add("("+bootStr + "42m)");
			Constant.colorRegExList.add("("+bootStr + "43m)");
			Constant.colorRegExList.add("("+bootStr + "44m)");
			Constant.colorRegExList.add("("+bootStr + "45m)");
			Constant.colorRegExList.add("("+bootStr + "46m)");
			Constant.colorRegExList.add("("+bootStr + "2;37;0m)");
			Constant.colorRegExList.add("("+bootStr + "0;0m)");




		}


		String regEx;
		for(String reg : Constant.colorRegExList){

			pattern = Pattern.compile(reg);
			matcher = pattern.matcher(src);
			if(matcher.find()){
				//System.out.println(matcher.group(1));
				//System.out.println(mColorMap.get(matcher.group(1)));
				regEx = matcher.group(1);
				if (regEx.contains("2;37;0m") || regEx.contains("0;0m")){
					src = matcher.replaceAll( "</font>" );
				}else{
					src = matcher.replaceAll( "<font color='"+mColorMap.get(regEx)+"'>" );
				}


				//matcher.group(1);


			}
		}
		/*
		regEx = "\\[2;37;0m";
		pattern = Pattern.compile(regEx);
		matcher = pattern.matcher(src);
		if(matcher.find()){
			src = matcher.replaceAll("</font>");
		}*/




		return src;


	}






	private static void insertColor() {
		// TODO Auto-generated method stub

		mColorList.add(BLK);
		mColorList.add(RED);
		mColorList.add(GRN);
		mColorList.add(YEL);
		mColorList.add(BLU);
		mColorList.add(MAG);
		mColorList.add(CYN);
		mColorList.add(WHT);

		mColorList.add(HIR);
		mColorList.add(HIG);
		mColorList.add(HIY);
		mColorList.add(HIB);
		mColorList.add(HIM);
		mColorList.add(HIC);
		mColorList.add(HIW);

		mColorList.add(HBRED);
		mColorList.add(HBGRN);
		mColorList.add(HBYEL);
		mColorList.add(HBBLU);
		mColorList.add(HBMAG);
		mColorList.add(HBCYN);
		mColorList.add(HBWHT);

		mColorList.add(BBLK);
		mColorList.add(BRED);
		mColorList.add(BGRN);
		mColorList.add(BYEL);
		mColorList.add(BBLU);
		mColorList.add(BMAG);
		mColorList.add(BCYN);
		mColorList.add(NOR);
	}

	private static void addColor() {

		mColorMap.put(ESC, CSI);
		mColorMap.put(CSI, CSI);
		mColorMap.put(BEL, CSI);

		mColorMap.put(BLK, "black");
		mColorMap.put(RED, "red");
		mColorMap.put(GRN, "green");
		mColorMap.put(YEL, "yellow");
		mColorMap.put(BLU, "blue");
		mColorMap.put(MAG, "magenta");
		mColorMap.put(CYN, "cyan");
		mColorMap.put(WHT, "white");

		mColorMap.put(HIR, "red");
		mColorMap.put(HIG, "green");
		mColorMap.put(HIY, "yellow");
		mColorMap.put(HIB, "blue");
		mColorMap.put(HIM, "magenta");
		mColorMap.put(HIC, "cyan");
		mColorMap.put(HIW, "white");

		mColorMap.put(HBRED, "red");
		mColorMap.put(HBGRN, "green");
		mColorMap.put(HBYEL, "yellow");
		mColorMap.put(HBBLU, "blue");
		mColorMap.put(HBMAG, "magenta");
		mColorMap.put(HBCYN, "cyan");
		mColorMap.put(HBWHT, "white");

		mColorMap.put(BBLK, "black");
		mColorMap.put(BRED, "red");
		mColorMap.put(BGRN, "green");
		mColorMap.put(BYEL, "yellow");
		mColorMap.put(BBLU, "blue");
		mColorMap.put(BMAG, "magenta");
		mColorMap.put(BCYN, "cyan");
		//颜色结束
		mColorMap.put(NOR, "NOR");

	}






	public static final String ESC	= "";
	public static final String CSI = ESC + "[";
	public static final String BEL = ESC + "[s";

	/* Foreground Colors */
	public static final String BLK = ESC + "[30m"; /* Black */
	public static final String RED = ESC + "[31m"; /* Red */
	public static final String GRN = ESC + "[32m"; /* Green */
	public static final String YEL = ESC + "[33m"; /* yellow */
	public static final String BLU = ESC + "[34m"; /* Blue */
	public static final String MAG = ESC + "[35m"; /* Magenta */
	public static final String CYN = ESC + "[36m"; /* Cyan */
	public static final String WHT = ESC + "[37m"; /* White */
	/* Hi Intensity Foreground Colors */
	public static final String HIR = ESC + "[1;31m"; /* Red */
	public static final String HIG = ESC + "[1;32m"; /* Green */
	public static final String HIY = ESC + "[1;33m"; /* Yellow */
	public static final String HIB = ESC + "[1;34m"; /* Blue */
	public static final String HIM = ESC + "[1;35m"; /* Magenta */
	public static final String HIC = ESC + "[1;36m"; /* Cyan */
	public static final String HIW = ESC + "[1;37m"; /* White */
	/* High Intensity Background Colors */
	public static final String HBRED = ESC + "[41;1m"; /* Red */
	public static final String HBGRN = ESC + "[42;1m"; /* Green */
	public static final String HBYEL = ESC + "[43;1m"; /* Yellow */
	public static final String HBBLU = ESC + "[44;1m"; /* Blue */
	public static final String HBMAG = ESC + "[45;1m"; /* Magenta */
	public static final String HBCYN = ESC + "[46;1m"; /* Cyan */
	public static final String HBWHT = ESC + "[47;1m"; /* White */
	/*  Background Colors  */
	public static final String BBLK = ESC + "[40m";	  /* Black    */
	public static final String BRED = ESC + "[41m";	  /* Red      */
	public static final String BGRN = ESC + "[42m";	  /* Green    */
	public static final String BYEL = ESC + "[43m";	  /* Yellow   */
	public static final String BBLU = ESC + "[44m";	  /* Blue     */
	public static final String BMAG = ESC + "[45m";	  /* Magenta  */
	public static final String BCYN = ESC + "[46m";	  /* Cyan     */

	public static final String NOR =  ESC + "[2;37;0m";      /* Puts everything back to normal */

	/**
	 * 特殊标签
	 */
	public static final String NOR2 =  ESC + "[0;0m";      /* Puts everything back to normal */

	//配色
	//绿色
	//public static final String DEFAULT_FONT_COLOR = "#009900";//Color.parseColor()
	//public static final String DEFAULT_FONT_COLOR = "#C5C1AA";
	//public static final String DEFAULT_BG_COLOR = "#000000";
	//public static final String DEFAULT_BR_COLOR = "#e67300";




	public static Map<String,IAppColor> appColor = new HashMap<String,IAppColor>();
	//用户自定义颜色
	public static AppColor userAppColor;

	//唯一调用接口


	public static  IAppColor getAppColor(){

		if(userAppColor==null){
			return Constant.appColor.get("c1");
		} else{
			return Constant.userAppColor;
		}

	}




	//获取默认se
	public static  IAppColor getDefaultAppColor(){

		return Constant.appColor.get("c1");


	}



	public static void setAppColor(AppColor userAppColor){
		Constant.userAppColor = userAppColor;
	}

	//针对数据库的初始化
	public static void initAppColor(Context c){



		String jsData="";

		if( c!=null ) {
			//先去本地缓存配色
			ColorConfModel ccm = new ColorConfModel(c);
			FFCursorHelper cursorHelper = new FFCursorHelper();
			cursorHelper.setCsr(ccm.getList());
			if (cursorHelper.getCsr().getCount() > 0) {
				cursorHelper.getCsr().moveToLast();
				jsData = cursorHelper.getString("colorstr");
				Constant.userAppColor = GsonManager.getIns().jsonToAnyObject(jsData, AppColor.class);
			}
			cursorHelper.getCsr().close();
		}
		//必须执行！！否则报错
		initAppColor();




	}


	public static void initAppColor(){
		AppColor apcolor = new AppColor();

		appColor.put("c1",
				new AppColor()
						.setBackColor("#000000")
						.setBorderColor("#8B8386")
						.setButBackColor("#101010")
						.setButBorderColor("#281910")
						.setLineColor("#303030")
						.setFontColor("#ffffff")
						.setAlertBoxBackColor("#222222")
						.setAlertBoxBorderColor("#555555")

		);
		//黑金
		appColor.put("c2",
				new AppColor()
						.setBackColor("#1d2228")
						.setBorderColor("#8B8386")
						.setButBackColor("#1d2228")
						.setButBorderColor("#ddc09c")
						.setLineColor("#ddc09c")
						.setFontColor("#ddc09c")
						.setAlertBoxBackColor("#665e54")
						.setAlertBoxBorderColor("#555555")

		);
		//书香
		appColor.put("c3",
				new AppColor()
						.setBackColor("#665e54")//665e54
						.setBorderColor("#8B8386")
						.setButBackColor("#665e54")
						.setButBorderColor("#ddc09c")
						.setLineColor("#ddc09c")
						.setFontColor("#ddc09c")
						.setAlertBoxBackColor("#ddc09c")
						.setAlertBoxBorderColor("#555555")

		);

		//深空
		appColor.put("c4",
				new AppColor()
						.setBackColor("#1d2228")//665e54
						.setBorderColor("#00EEEE")
						.setButBackColor("#1d2228")
						.setButBorderColor("#00EEEE")
						.setLineColor("#00EEEE")
						.setFontColor("#00EEEE")
						.setAlertBoxBackColor("#15191e")
						.setAlertBoxBorderColor("#555555")

		);



	}


}
